package com.fjec.gateway.filter;

import com.fjec.common.enums.EnumSysCode;
import com.fjec.common.exception.BizException;
import com.fjec.common.protocol.JsonResult;
import com.fjec.common.util.JsonUtil;
import com.google.common.base.Strings;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authz.AuthorizationFilter;
import org.apache.shiro.web.util.WebUtils;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;

public class MyRolesFilter extends AuthorizationFilter {

    @Override
    protected boolean isAccessAllowed(ServletRequest servletRequest, ServletResponse servletResponse, Object o) throws Exception {
        String[] arr = (String[]) o;
        if (arr == null || arr.length == 0) {
            //没有角色限制，有权限访问
            return true;
        }
        Subject subject = getSubject(servletRequest, servletResponse);
        for (String role : arr) {
            if (subject.hasRole(role)) {
                //或
                return true;
            }
        }
        return false;
    }

    @Override
    protected boolean onAccessDenied(ServletRequest req, ServletResponse res) throws IOException {
        throw new BizException(EnumSysCode.ERR_0004.getCode(), EnumSysCode.ERR_0004.getMsg());
    }
}
